<template>
	<view class="content">
		<image mode="widthFix" style="width: 100%;" :src="imageUrl+'/job_hunting/index/bg.png'" class="bg-img"></image>
		<view class="welcome-box">
			<view class="welcome-text">Welcome</view>
			<view class="welcome-text" style="font-size:54rpx;font-weight:bold;">进入码上就业</view>
		</view>
		<view class="theme">
			<view class="agreement-block">
				<checkbox-group @change="checkChange">
					<checkbox color="#FF7626" id="check" class="checkbox" /><label for="check"
						class="radio-label">我已阅读并同意<text class="agreement" @click="navigate('/page_other/user/law')">
							《用户服务协议》</text><text class="agreement"
							@click="navigate('/page_other/user/privacy')">《个人信息保护政策》</text>，未注册的手机号将自动完成注册</label>
				</checkbox-group>
			</view>
			<view>
				<button size="mini" type="primary" class="entrance1" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">手机号码一键登录</button>
			</view>
			<view @click="navigate('/page_other/login/index')" class="entrance2">
				验证码登录
			</view>

		</view>

	</view>
</template>

<script>
	import {
		isLogin
	} from '@/util/user.js';
	import {
		getStorage
	} from '@/util/auth.js'
	import {
		validate
	} from '@/util/validate.js'
	export default {
		data() {
			return {
				imageUrl: '',
				form: {
					checked: [],
					type: '',
					phoneInfo: '',
					userCode: ''
				},
				rules: {
					checked: {
						name: '',
						required: false,
						validator: function(value, form, callback) {
							if (value.length == 0) {
								callback('请勾选同意协议！');
							}
							callback();
						}
					},
				},
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			checkChange(e) {
				this.form.checked = e.detail.value;
			},
			/**
			 *
			 * 获取用户信息
			 */
			userLogin() {
				uni.login({
					provider: 'weixin',
					success: async (res) => {
						// console.log('登录获取code', res.code);
						if (res.errMsg == 'login:ok') {
							this.userCode = res.code;
							console.log(this.userCode)

						}
					},
				});
			},
			requestSubscribeMessage() {
				wx.requestSubscribeMessage({ //获取下发权限
					tmplIds: ['SbpnxgSI_bHXA-GCrmMjgdG5W5TLB89O5qIwk1Shfy8',
						's3VZnlhwZGXmlebBGTovtq9JJEzu4h92B1B2PL0k0-g',
						'XMGjlF0XxYMdGUWH5C25Qv8sdY9Ffg2c6WYdSqY5DuE'
					],
					success(res) {
						console.log('已授权接收订阅消息')
					},
					fail(err) {
						console.error('订阅消息发送失败', err)
					}

				});
			},


			getPhoneNumber(e) {
				if (validate(this.form, this.rules)) {
					this.form.phoneInfo = e.detail.code;
					if (this.form.phoneInfo) {
						uni.showLoading({
							title: '提交中',
							mask: true,
						})
						this.form.type = this.$store.getters.identity;
						this.form.userCode = this.userCode
						this.$store.dispatch('user/login', this.form).then(() => {
							uni.showLoading({
								title: '登录中',
								mask: true
							})
							this.$store.dispatch('user/loginIM').then(() => {
								const user = this.$store.getters.user;
								if (getStorage('identity') == 2 && user.authenticationStatus == 3) {
									uni.navigateTo({
										url: '/page_other/enterprise/authorization/examine'
									})
								} else {
									this.showToast('登录成功', () => {
										uni.switchTab({
											url: '/pages/mine/index'
										})
									});
								}
							})
						})
					}
				}
			}

		},
		onLoad() {
			this.imageUrl = this.globalConfig.imageUrl;
			this.userLogin()
			if (isLogin()) { //如果已经登录则跳转至首页
				uni.switchTab({
					url: '/pages/home/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}

	.content .bg-img {
		width: 100%;
		position: absolute;
		top: 0;
	}

	.content .welcome-box {
		width: 85%;
		padding: 30rpx 0 0 7.5%;
		position: relative;

		.welcome-text {
			font-size: 36rpx;
			color: #fff;
			padding: 10rpx 0;
		}
	}



	.content .theme {
		margin-top: 100rpx;
		background: #fff;
		width: 100%;
		position: absolute;
		border-radius: 50rpx;

		.agreement-block {
			width: 100%;
			margin-top: 40rpx;
			display: flex;
			flex-direction: row;
			padding: 30rpx;

			.radio-label {
				font-size: 24rpx;

				.agreement {
					color: #FB093B;
				}
			}

			.checkbox {
				transform: scale(0.7);
			}
		}

		.entrance1 {
			width: 75%;
			height: 100rpx;
			margin: 50rpx auto;
			background-color: #fff;
			border-radius: 36rpx;
			background-color: #FB093B;
			color: #fff;
			overflow: hidden;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: center;
			position: relative;
		}

		.entrance2 {
			width: 75%;
			height: 100rpx;
			margin: 5rpx auto;
			// background-color: #fff;
			border-radius: 36rpx;
			border: 1rpx solid #999;
			// box-shadow: 0px 0px 4px #ccc;
			overflow: hidden;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: center;
			position: relative;
		}
	}
</style>